//
//  ViewController.m
//  TheGame2048
//
//  Created by User-23 on 11/10/14.
//  Copyright (c) 2014 TrainingCamp. All rights reserved.
//

#import "ViewController.h"
#import "PlayCell.h"
#import "NumberMatrix.h"


@interface ViewController ()


@property (weak, nonatomic) IBOutlet UICollectionView *colectionViewPlayground;

@property NumberMatrix* matrix;

@property BOOL gameWon;
@end

static const NSInteger kWiningNumber = 128;

static const NSInteger kPlayArraySize = 4;

@implementation ViewController

// columns
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return kPlayArraySize;
}

// rows
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return kPlayArraySize;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString* cellID = @"CustomCell";
    
    PlayCell* cell = [self.colectionViewPlayground dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor greenColor];
    NSArray* myarr = [self.matrix output];
    //NSLog(@"%@",[[myarr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]);
    
    cell.forNumber.text = [NSString stringWithFormat:@"%@",[[myarr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];
    //NSNumber* cellNumber = [NSNumber numberWithInteger:cell.forNumber.text];
    if ([cell.forNumber.text integerValue] <= 1 ) {
        cell.backgroundColor = [UIColor clearColor];
        cell.forNumber.text = @"";
        
    }
    
    if ([cell.forNumber.text integerValue] == kWiningNumber ) {
        if (!self.gameWon) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"YOU WON THE GAME"
                                                            message:@"Жестоко нали"
                                                           delegate:self
                                                  cancelButtonTitle:@"ЖЕСТОКО!"
                                                  otherButtonTitles:nil];
            [alert show];
            self.gameWon = YES;
        }
    }
    
    if ([cell.forNumber.text integerValue] == 2 ) {
        cell.backgroundColor = [UIColor colorWithRed:237.0f/255.0f green:246.0f/255.0f blue:147.0f/255.0f alpha:1.0f];
    }
    if ([cell.forNumber.text integerValue] >= 4 ) {
        cell.backgroundColor = [UIColor colorWithRed:237.0f/255.0f green:246.0f/255.0f blue:100.0f/255.0f alpha:1.0f];
    }
    if ([cell.forNumber.text integerValue] >= 8 ) {
        cell.backgroundColor = [UIColor colorWithRed:237.0f/255.0f green:213.0f/255.0f blue:147.0f/255.0f alpha:1.0f];
    }
    if ([cell.forNumber.text integerValue] >= 16 ) {
        cell.backgroundColor = [UIColor colorWithRed:237.0f/255.0f green:213.0f/255.0f blue:95.0f/255.0f alpha:1.0f];
    }
    if ([cell.forNumber.text integerValue] >= 32 ) {
        cell.backgroundColor = [UIColor colorWithRed:237.0f/255.0f green:145.0f/255.0f blue:95.0f/255.0f alpha:1.0f];
    }
    if ([cell.forNumber.text integerValue] >= 64 ) {
        cell.backgroundColor = [UIColor colorWithRed:237.0f/255.0f green:74.0f/255.0f blue:195.0f/255.0f alpha:1.0f];
    }
    if ([cell.forNumber.text integerValue] >= 128 ) {
        cell.backgroundColor = [UIColor colorWithRed:237.0f/255.0f green:74.0f/255.0f blue:207.0f/255.0f alpha:1.0f];
    }
    if ([cell.forNumber.text integerValue] >= 256 ) {
        cell.backgroundColor = [UIColor colorWithRed:138.0f/255.0f green:228.0f/255.0f blue:92.0f/255.0f alpha:1.0f];
    }
    if ([cell.forNumber.text integerValue] >= 512 ) {
        cell.backgroundColor = [UIColor colorWithRed:27.0f/255.0f green:228.0f/255.0f blue:156.0f/255.0f alpha:1.0f];
    }
    if ([cell.forNumber.text integerValue] >= 1024 ) {
        cell.backgroundColor = [UIColor colorWithRed:27.0f/255.0f green:129.0f/255.0f blue:234.0f/255.0f alpha:1.0f];
    }
    if ([cell.forNumber.text integerValue] >= 2048 ) {
        cell.backgroundColor = [UIColor colorWithRed:27.0f/255.0f green:19.0f/255.0f blue:234.0f/255.0f alpha:1.0f];
    }
    
    return cell;
}

// left
-(void)swipeHandlerLeft{
    
    [self.matrix sumInDirection:@"Left"];
    [self.playGround reloadData];
    
}
// right
-(void)swipeHandlerRight{
    [self.matrix sumInDirection:@"Right"];
    [self.playGround reloadData];
}
// up
-(void)swipeHandlerUp{
    [self.matrix sumInDirection:@"Up"];
    [self.playGround reloadData];
}
// down
-(void)swipeHandlerDown{
    [self.matrix sumInDirection:@"Down"];
    [self.playGround reloadData];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // left
    UISwipeGestureRecognizer *gestureRecognizerLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandlerLeft)];
    [gestureRecognizerLeft setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [self.view addGestureRecognizer:gestureRecognizerLeft];
    // right
    UISwipeGestureRecognizer *gestureRecognizerRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandlerRight)];
    [gestureRecognizerRight setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [self.view addGestureRecognizer:gestureRecognizerRight];
    //up
    UISwipeGestureRecognizer *gestureRecognizerUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandlerUp)];
    [gestureRecognizerUp setDirection:(UISwipeGestureRecognizerDirectionUp)];
    [self.view addGestureRecognizer:gestureRecognizerUp];
    // down
    UISwipeGestureRecognizer *gestureRecognizerDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandlerDown)];
    [gestureRecognizerDown setDirection:(UISwipeGestureRecognizerDirectionDown)];
    [self.view addGestureRecognizer:gestureRecognizerDown];
    
    [self.colectionViewPlayground registerNib:[UINib nibWithNibName:@"PlayCell" bundle:nil] forCellWithReuseIdentifier:@"CustomCell"];
    
    self.colectionViewPlayground.dataSource = self;
    self.colectionViewPlayground.delegate = self;
    
    // initialized just onse at begining
    self.matrix = [[NumberMatrix alloc]init];
    self.gameWon = NO;
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
